faApp.controller('UserListCtrl',['$scope','userService','$location','$controller',function($scope,userService,$location,$controller) {   
  $controller('BaseCtrl', {$scope: $scope});
  $scope.list = [];
  function getData(id){
    userService.userList({
      layerPath:id
    }).then(function(json){
      $scope.list = json.list || [];
    });
  }
  getData();
  userService.userListTree().then(function(json){
    console.log(json);
    //$scope.list = json.list || [];
    $(document).ready(function(){
      $.fn.zTree.init($("#treeDemo"), setting, json.list);
    });
  });
  var setting = {
    data: {
      simpleData: {
        enable: true
      }
    },
    callback:{ //回调函数
      /**
       * event:鼠标事件
       * treeId：树的容器id
       * treeNode：当前点击的节点
       */
      beforeClick:function(treeId,treeNode,clickFlag){
        console.log(treeNode);
        getData(treeNode.id);

      }
    }
  };

  //$scope.cktest = function(){

  //}





}]);

faApp.controller('UserAddCtrl',['$scope','userService','$location','$controller','authService',function($scope,userService,$location,$controller,authService) {   
	$controller('BaseCtrl', {$scope: $scope}); 

  $scope.item = {status:"0",userType:"02",sex:"1"};
  $scope.roleList = [];
  $scope.selectRoles = [];
  $scope.allRoles = {};

  authService.roleList().then(function(json){
    $scope.roleList = json.allGroup;  
  });
  var setting = {
    data: {
      simpleData: {
        enable: true
      }
    },
    callback:{ //回调函数
      /**
       * event:鼠标事件
       * treeId：树的容器id
       * treeNode：当前点击的节点
       */
      beforeClick:function(treeId,treeNode,clickFlag){

        console.log(treeNode);
        console.log($scope.selectedName);

        if($scope.selectedName == '1'){
          $scope.item.parentUserId = treeNode.id;
          $scope.item.parentUserName = treeNode.name;
        }else if($scope.selectedName == '2'){
          $scope.item.deptHeadName = treeNode.name;
          $scope.item.deptHead = treeNode.id;
        }else if($scope.selectedName == '3'){
          $scope.item.publicDeptHeadName = treeNode.name;
          $scope.item.publicDeptHead = treeNode.id;
        }

        $scope.$apply();
      }
    }
  };
  //获取部门列表
  $scope.getDepartment = function(){
    userService.userListTree().then(function(json){
      console.log(json);
      //$scope.list = json.list || [];
      $(document).ready(function(){
        $.fn.zTree.init($("#treeDemo"), setting, json.list);
      });
    });
  };
  $scope.getDepartment();

  $scope.save = function(){
    if ($scope.form.$valid){
      
      if(($scope.item.telePhone + "").length != 11){
        alert("手机号长度必须为11位！");
        return;
      }
      if(($scope.item.telePhone + "").substr(0,1) != "1"){
        alert("手机号第一位必须是1!");
        return;
      }

      var flag = false;
      for (var i in $scope.allRoles){
        if ($scope.allRoles[i]){
          flag = true;
          $scope.selectRoles.push(i);
        }
      }
      if (!flag){
        alert("请先选择权限");
        return;
      }
      $scope.item.groupId = $scope.selectRoles.join(",");
      delete $scope.item.parentUserId_name;

      userService.userAdd($scope.item).then(function(json){
        if (json.statusCode){
          alert("出错了:" + json.statusCode);
          return;
        }else{
          $scope.alertNextPage("新增管理员成功！");
          $location.path("/user/list");
        }  
      });
    }else{
      alert("请先完整填写表单");
    }
  };
}]);

faApp.controller('UserEditCtrl',['$scope','userService','$location','$controller','$routeParams','authService',function($scope,userService,$location,$controller,$routeParams,authService) {   
	$controller('BaseCtrl', {$scope: $scope}); 

  $scope.item = {};
  $scope.roleList = [];
  $scope.selectRoles = [];
  $scope.allRoles = {};
  $scope.id = $routeParams.id;

  authService.roleList().then(function(json){
    $scope.roleList = json.allGroup;  
  });
  var setting = {
    data: {
      simpleData: {
        enable: true
      }
    },
    callback:{ //回调函数
      /**
       * event:鼠标事件
       * treeId：树的容器id
       * treeNode：当前点击的节点
       * isParent 是否为父节点
       */
      beforeClick:function(treeId,treeNode,clickFlag){

        console.log(treeNode);
        //isParent 是否为父节点
        console.info(treeNode.isParent);

        if($scope.selectedName == '1'){
          $scope.item.parentUserId = treeNode.id;
          $scope.item.parentUserName = treeNode.name;
        }else if($scope.selectedName == '2'){
          $scope.item.deptHeadName = treeNode.name;
          $scope.item.deptHead = treeNode.id;
        }else if($scope.selectedName == '3'){
          $scope.item.publicDeptHeadName = treeNode.name;
          $scope.item.publicDeptHead = treeNode.id;
        }

        $scope.$apply();
      }
    }
  };
  //获取部门列表

  userService.userListTree().then(function(json){
    console.log(json);
    //$scope.list = json.list || [];
    $(document).ready(function(){
      $.fn.zTree.init($("#treeDemo"), setting, json.list);
    });
  });

  userService.userQuery($scope.id).then(function(json){
    $scope.item = json.userInfo;
    $scope.item.telePhone = parseInt(json.userInfo.telePhone);
    angular.forEach(json.selectGroups,function(o){
      $scope.allRoles[o.groupId] = true;  
    }); 
  });

  $scope.save = function(){
    if ($scope.form.$valid){
      if(($scope.item.telePhone + "").length != 11){
        alert("手机号长度必须为11位！");
        return;
      }
      if(($scope.item.telePhone + "").substr(0,1) != "1"){
        alert("手机号第一位必须是1!");
        return;
      }

      var flag = false;
      for (var i in $scope.allRoles){
        if ($scope.allRoles[i]){
          flag = true;
          $scope.selectRoles.push(i);
        }
      }
      if (!flag){
        alert("请先选择权限");
        return;
      }
      $scope.item.groupId = $scope.selectRoles.join(",");
      console.log($scope.item);

      userService.userEdit($scope.item).then(function(json){
        if (json.statusCode){
          alert("出错了:" + json.statusCode);
          return;
        }else{
          $scope.alertNextPage("新增管理员成功！");
          $location.path("/user/list");
        }  
      });
    }else{
      alert("请先完整填写表单");
    }
  };
  
   

}]);
